package cn.wolfcode.p2p.base.service.impl;

import cn.wolfcode.p2p.base.domain.Account;
import cn.wolfcode.p2p.base.domain.IpLog;
import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.exception.DisplayableException;
import cn.wolfcode.p2p.base.mapper.LoginInfoMapper;
import cn.wolfcode.p2p.base.service.IAccountService;
import cn.wolfcode.p2p.base.service.IIpLogService;
import cn.wolfcode.p2p.base.service.ILoginInfoService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.util.*;
import cn.wolfcode.p2p.base.vo.VerifyCodeVo;
import cn.wolfcode.p2p.business.domain.ExpAccount;
import cn.wolfcode.p2p.business.domain.ExpAccountGrantRecord;
import cn.wolfcode.p2p.business.service.IExpAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class LoginInfoServiceImpl implements ILoginInfoService {

    @Autowired
    private LoginInfoMapper loginInfoMapper;
    @Autowired
    private IAccountService accountService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IIpLogService iIpLogInfoService;
    @Autowired
    private IExpAccountService expAccountService;

    @Override
    public boolean checkPhoneNumber(String username) {
        return loginInfoMapper.queryCountByUserName(username) <= 0;
    }

    @Override
    public LoginInfo get(Long auditorId) {
        return loginInfoMapper.selectByPrimaryKey(auditorId);
    }

    @Override
    public List<LoginInfo> queryAuditorList() {
        return loginInfoMapper.queryAuditorList();
    }

    @Override
    public void initAdmin() {
        int count = loginInfoMapper.queryCountByUserType(LoginInfo.USERTYPE_MANAGER);
        if (count == 0) {
            LoginInfo loginInfo = new LoginInfo();
            loginInfo.setUsername(Constants.DEFALUT_ACCOUNT);
            loginInfo.setPassword(MD5.encoder(Constants.DEFALUT_PASSWORD));
            loginInfo.setUserType(LoginInfo.USERTYPE_MANAGER);
            loginInfoMapper.insert(loginInfo);
        }
    }

    @Override
    public LoginInfo userLogin(String username, String password, int userType) {
        LoginInfo loginInfo = loginInfoMapper.userLogin(username, MD5.encoder(password), userType);
        IpLog ipLog = new IpLog();
        ipLog.setIp(UserContext.getIp());
        ipLog.setLoginTime(new Date());
        ipLog.setUsername(username);
        ipLog.setUserType(userType);
        if (loginInfo == null) {
//            throw new DisplayableException("账号或密码错误");
            ipLog.setState(IpLog.STATE_FAILED);
        } else {
            ipLog.setState(IpLog.STATE_SUCCESS);
            //把登录信息传到session中
            UserContext.setCurrent(loginInfo);
        }
        iIpLogInfoService.save(ipLog);
        return loginInfo;
    }

    @Override
    public void register(String username, String verifycode, String password, String confirmPwd) {
        //对传入的参数进行校验
        AssertUtil.isNull(username, "用户名不能为空");
        AssertUtil.isNull(verifycode, "验证码不能为空");
        AssertUtil.isNull(password, "密码不能为空");
        AssertUtil.isNull(confirmPwd, "确认密码不能为空");

        //对验证码进行校验 TODO
        VerifyCodeVo vo = UserContext.getVerifyCodeVO();
        if (vo == null || !vo.getRandomCode().equals(verifycode)) {
            throw new DisplayableException("验证码输入不正确");
        }
        if (!vo.getPhoneNumber().equals(username)) {
            throw new DisplayableException("非法操作");
        }
        if (DateUtil.getBetweenTime(vo.getSendTime(), new Date()) > Constants.VERIFYCODE_VALID_TIME * 60) {
            throw new DisplayableException("验证码过期,请重新发送");
        }

        //判断两次密码是否一致
        if (!password.equals(confirmPwd)) {
            throw new DisplayableException("两次密码不一致");
        }
        //判断手机号码是否已经注册了
        int count = loginInfoMapper.queryCountByUserName(username);
        if (count > 0){
            throw new DisplayableException("该手机号码已经被注册了");
        }
        //插入注册信息到数据库
        LoginInfo info = new LoginInfo();
        info.setUsername(username);
        info.setPassword(MD5.encoder(password)); //不要忘记加密
        info.setUserType(LoginInfo.USERTYPE_USER);
        loginInfoMapper.insert(info);
        //创建account和userinfo信息
        Account account = new Account();
        account.setId(info.getId());
        accountService.insert(account);
        UserInfo userInfo = new UserInfo();
        userInfo.setId(info.getId());
        userInfo.setPhoneNumber(username);
        userInfoService.insert(userInfo);

        //创建体验金账户
        ExpAccount expAccount = new ExpAccount();
        expAccount.setId(info.getId());
        expAccountService.save(expAccount);

        //发放体验金
        expAccountService.grantExpMoney(expAccount.getId(), new IExpAccountService.LastTime(1,IExpAccountService.LastTimeUnit.MONTH),
                Constants.REGISTER_GRANT_EXPMONEY, Constants.EXPMONEY_TYPE_REGISTER);
    }
}
